<template>
  <div class="app-container">
    <el-form ref="form" :inline="true" :model="queryParams" class="demo-form-inline">
      <el-form-item label="配置key" prop="constantKey">
        <el-input v-model="queryParams.constantKey" placeholder="配置key" />
      </el-form-item>
      <el-form-item label="配置名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="配置名称" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button type="primary" @click="reset">重置</el-button>
        <!-- <el-button type="primary" @click="onAdd">新增</el-button> -->

      </el-form-item>
    </el-form>

    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column align="center" label="序号" width="95">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>

      <el-table-column label="配置key" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.constantKey }}</span>
        </template>
      </el-table-column>

      <el-table-column label="配置名称" align="center">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="handleAdd(scope.row)">查看</el-button>
          <el-button type="text" @click="handleEdit(scope.row)">修改</el-button>

        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination :current-page.sync="queryParams.pageNum" :page-sizes="[10, 20, 30, 40]"
        :page-size.sync="queryParams.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"
        @size-change="fetchData" @current-change="fetchData" />
    </div>
    <showInfo ref="showInfo" />
    <el-dialog title="修改配置" :visible.sync="dialogVisible" width="500px">
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="配置key" prop="name">
          <el-input v-model="ruleForm.name" placeholder="配置key" />
        </el-form-item>
        <el-form-item label="配置内容" prop="data">
          <el-input v-model="ruleForm.data" placeholder="配置内容" />
        </el-form-item>
        <el-form-item label="配置项名称" prop="constantKey">
          <el-input v-model="ruleForm.constantKey" placeholder="配置项名称" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { constants, update } from '@/api/table'
import showInfo from './showInfo.vue'
export default {
  components: {
    showInfo
  },
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      dialogVisible: false,
      queryParams: {
        'pageNum': 0,
        'pageSize': 10,
        constantKey: '',
        name: ''
      },
      ruleForm: {
        data: '',
        name: '',
        constantKey: '',
        id: ''

      },
      rules: {
        data: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        constantKey: [
          { required: true, message: '请输入key', trigger: 'blur' }
        ]

      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      constants(this.queryParams).then(response => {
        this.list = response.data.records
        this.listLoading = false
        this.total = response.data.total
      })
    },
    handleEdit(e) {
      this.dialogVisible = true
      this.ruleForm.name = e.name
      this.ruleForm.data = e.data
      this.ruleForm.constantKey = e.constantKey
      this.ruleForm.id = e.id
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          update(this.ruleForm).then(res => {
            this.$message('提交成功!')

            this.dialogVisible = false
            this.fetchData()
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleShowInfo(e) {
      this.$refs.showInfo.show(e)
    },

    onSubmit() {
      this.queryParams.pageNum = 1
      this.fetchData()
    },
    reset() {
      this.$refs.form.resetFields()
      this.fetchData()
    },
    handleAdd(e) {
      this.$refs.showInfo.show(e)
    }

  }
}
</script>
